package com.idbk.chargestation.util;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;

import com.idbk.chargestation.R;
import com.idbk.chargestation.activity.user.ActivityMyMessage;

/**
 * 通知栏工具类
 * 文档参考地址：https://developer.android.google.cn/guide/topics/ui/notifiers/notifications.html
 * Created by lupc on 2017/6/14.
 */

public class NotificationUtil {

    private final static int ID_NOTIFICATION = 1;

    public static void showString(Context context, String msg) {
        CharSequence contentTitle = context.getResources().getText(R.string.app_name);

        NotificationCompat.Builder mBuilder =
                new  NotificationCompat.Builder(context)
                        .setAutoCancel(true) //点击时候自动删除通知
                        .setSmallIcon(R.drawable.push)
                        .setContentTitle(contentTitle)
                        .setContentText(msg);
        // Creates an explicit intent for an Activity in your app
        Intent resultIntent = new Intent(context, ActivityMyMessage.class);

        // The stack builder object will contain an artificial back stack for the
        // started Activity.
        // This ensures that navigating backward from the Activity leads out of
        // your application to the Home screen.
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        // Adds the back stack for the Intent (but not the Intent itself)
        stackBuilder.addParentStack(ActivityMyMessage.class);
        // Adds the Intent that starts the Activity to the top of the stack
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                        0,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        // mId allows you to update the notification later on.
        mNotificationManager.notify(ID_NOTIFICATION, mBuilder.build());
    }

}
